﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MemberPoint.Entity.POCOModels;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;

namespace MemberPoint.Business
{
    /// <summary>
    /// 从线程中获取数据访问上下文，保证线程内实例唯一
    /// </summary>
    public class ContextFactory
    {
        public static DbContext GetCurrentContext()
        {
            //从线程中取DbContext
            DbContext context = CallContext.GetData("Member") as DbContext;
            //如果取不到
            if (context == null) 
            {
                //直接从Member中取DbContext
                context = new Member();
                //存入线程DbContext中
                CallContext.SetData("Member", context);
            }
            return context;
        }
    }
}
